//
//  RightCollectionClickController.m
//  quickLook
//
//  Created by J on 15/9/1.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "RightCollectionClickController.h"
#import "Header.h"
#import "ThirdNaviSearchController.h"
#import "NavisearchCell.h"
@interface RightCollectionClickController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _number;
    BOOL _isLoad;
    
    
}
@property(nonatomic,assign)BOOL isLoad;

@end

@implementation RightCollectionClickController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self loadData];
    
    [self createUI];

}

- (void)loadData
{

    
    if (_number==0) {
        //下拉刷新
        
        self.dataArray=[NSMutableArray array];
        
        [_tableView.header endRefreshing];
        
    }else{
        [_tableView.footer endRefreshing];
    }
    

    [DownLoad getRightSearchData:_number andTag:[_navigationTitle URLEncode] withBlock:^(id obj, NSError *error) {
        
        
        NSArray* values=obj[@"data"][@"topics"];
        
        [values enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger idx, BOOL *stop) {
            

        Model* model=[[Model alloc]init];

            
            
            [model setValuesForKeysWithDictionary:dic];
            

            
            [_dataArray addObject:model];
            
            
            
            
        }];
        
        
        [_tableView reloadData];
        
        
    }];


}



- (void)createUI
{
    
    self.title=[NSString stringWithFormat:@"%@",_navigationTitle];
    self.automaticallyAdjustsScrollViewInsets=NO;

    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    
    [self.view addSubview:_tableView];
    
    
    
    
    
    
    WS(weakSelf)

    
    //添加下拉刷新
    [weakSelf.tableView addLegendHeaderWithRefreshingBlock:^{// 每次下拉都会产生效果  所以需要一次操作来停止效果
        if (!weakSelf.isLoad) {
            //开始数据请求
            _number=0;
            weakSelf.isLoad=!weakSelf.isLoad;
            [weakSelf loadData];
        }
        else
        {
            [weakSelf.tableView.header endRefreshing];//如果正在运行 的时候拉一下会进入else但是 会产生效果   需要停止这个效果 每次下拉都会产生效果  所以需要一次操作来停止效果
            
        }
    }];
    
    
    
    
    
    

    //添加上拉加载
    [_tableView addLegendFooterWithRefreshingBlock:^{
        
       
        if (!weakSelf.isLoad)//如果没有加载为真 进入方法
        {
            _number=_number+20;
            weakSelf.isLoad=!weakSelf.isLoad;//加载状态
            [weakSelf loadData];//异步执行完完毕之后会变成未加载  否则再次下拉进入else方法
            
//            JZJLog(@"%zd",_number);
        }else{//异步请求执行完毕之后isLoad才会变成NO 否则再次下拉会一直执行这个方法
            [weakSelf.tableView.footer endRefreshing];
            
        }
        
        
    }];
    

    
    
}



#pragma mark --UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
//    JZJLog(@"********************%zd",_dataArray.count);
    
    
    return _dataArray.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NavisearchCell * cell=[tableView dequeueReusableCellWithIdentifier:@"ID"];
    
    if (!cell) {
        
        cell=[[NSBundle mainBundle]loadNibNamed:@"NavisearchCell" owner:nil options:nil][0];
        
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
        
        _tableView.tableFooterView = [[UIView alloc]init];

        
    }
    
 
    //刷新
    
    Model* model=_dataArray[indexPath.row];
    [cell refreshWithModel:model WithIndexPath:indexPath];
    
    
    
    
    
    
    return cell;
    
}


#pragma mark --UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    ThirdNaviSearchController* vc=[[ThirdNaviSearchController alloc]init];
    
    
    Model* model=_dataArray[indexPath.row];    
    
    vc.model=model;
    
    [self.navigationController pushViewController:vc animated:YES];





}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
